package com.zj.project.common.base

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import com.zj.project.BR
import com.zj.project.bean.ArticleItemBean
import com.zj.project.databinding.AdapterPagingBinding

class BasePagingAdapter :
    PagingDataAdapter<ArticleItemBean, BaseViewHolder<AdapterPagingBinding>>(object :
        DiffUtil.ItemCallback<ArticleItemBean>() {
        override fun areItemsTheSame(oldItem: ArticleItemBean, newItem: ArticleItemBean): Boolean {
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(
            oldItem: ArticleItemBean,
            newItem: ArticleItemBean
        ): Boolean {
            return oldItem == newItem
        }
    }) {
    override fun onBindViewHolder(holder: BaseViewHolder<AdapterPagingBinding>, position: Int) {
        val binding = DataBindingUtil.getBinding<AdapterPagingBinding>(holder.itemView)
        binding?.setVariable(BR.item, getItem(position))
        binding?.executePendingBindings()
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): BaseViewHolder<AdapterPagingBinding> {
        return BaseViewHolder(
            AdapterPagingBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )
    }


}